package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        // 判断书架容量是否满了
        if (bookList.getUsedSize() == bookList.getBooks().length) {
            System.out.println("书架已满");
            return;
        }

        // 展示图书
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }

        System.out.println("新增图书");
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入图书名称：");
        String name = scanner.nextLine();

        System.out.println("请输入图书作者：");
        String author = scanner.nextLine();

        System.out.println("请输入图书价格：");
        int price = scanner.nextInt();

        System.out.println("请输入图书类型：");
        String type = scanner.next();

        System.out.println("请输入图书id：");
        int id = scanner.nextInt();

        // 判断是否存在该书名或id
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if (bookList.getBook(i).getName().equals(name) || bookList.getBook(i).getId() == id) {
                System.out.println("图书已存在");
                return;
            }
        }

        bookList.setBook(bookList.getUsedSize(), new Book(name, author, price, type, id));
        bookList.setUsedSize(bookList.getUsedSize() + 1);
    }
}
